package com.ggboy.sc.pl.project.es.util;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.apache.commons.lang.StringUtils;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.LinkedList;

@Component("ikUtils")
public class IkUtils {

    @Resource(name = "httpUtils")
    private HttpUtils httpUtils;
//    @Resource(name = "esInfo")
//    private EsInfo esInfo;

    /**
     * 根据输入内容，进行分司
     *
     * @param queryString
     * @return
     * @throws Exception
     */
    public LinkedList<String> queryIk(String queryString) throws Exception {
        if (StringUtils.isEmpty(queryString)) {
            return new LinkedList<String>();
        }
//        String params;
//        params = java.net.URLEncoder.encode("合肥市汽车美容行业文本流黄队", "UTF-8");
//        params = java.net.URLEncoder.encode(queryString, "UTF-8");
        LinkedList<String> linkedList = new LinkedList<String>();
//        List<String> analyzeKeywords = new ArrayList<String>();
        String bodys="";
//        bodys = httpUtils.sendGet(esInfo.getIkurl().concat(java.net.URLEncoder.encode(queryString, "UTF-8")), "");
        if (null != bodys && StringUtils.isNotEmpty(bodys)) {
            JSONObject jSONObject = JSONObject.parseObject(bodys);
            JSONArray tokens = jSONObject.getJSONArray("tokens");
            if (tokens.size() > 0) {
                for (int i = 0; i < tokens.size(); i++) {
                    JSONObject token = (JSONObject) tokens.get(i);
                    if (StringUtils.isNotEmpty(token.getString("token"))) {
                        if (!linkedList.contains(token.getString("token"))) {
                            linkedList.add(token.getString("token"));
                        }

//                        String body3 = new String(token.getString("token").getBytes("UTF-8"), "UTF-8");
//                        linkedList.add(body3);
//                        System.out.println(body3);
                    }
                }
            }
        }
        return linkedList;
    }

}
